<?php
/**
 * Profile.php
 *
 * 系统全局状态数据映射类.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Model
 * @subpackage 	Mapper
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Model_Mapper_Abstract */
//require_once 'Abstract.php';

/**
 * 系统全局状态数据映射类.
 *
 * @category 	Momoger
 * @package		Momoger_Model
 * @subpackage 	Mapper
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Model_Mapper_Profile extends Momoger_Model_Mapper_Abstract {

    /**
     * 保存模型数据.
     *
     * @param Momoger_Model_Abstract $model 系统状态信息模型
     * @return Momoger_Model_Profile
     */
    public function save(Momoger_Model_Abstract $model) {
        $key = $model->get('pName');
        if($key) {
        	$row = $this->getDao()->fetchRow("pName = '$key'");
        	if ($row) {
        		$row->pValue = $model->get('pValue');
        		$row->pDesc = $model->get('pDesc');
        		$row->updated = time();
        		$row->save();
        		unset($row);
        	} else {
        		return $this->insert($model);
        	}
        }
		return $model;
    }

    /**
     * 保存新模型数据
     *
     * @param Momoger_Model_Abstract $model 系统状态信息模型
     * @return Momoger_Model_Profile
     */
    public function insert(Momoger_Model_Abstract $model) {
    	$key = $model->get('pName');
    	if ($key) {
        	$init = array('pValue' => '', 'pDesc' => '', 'updated' => time());
        	$data = array_merge($init, $model->getAll());
        	$this->getDao()->insert($data);
        	$model->set($data);
    	}
    	return $model;
    }

    /**
     * 更新模型数据
     *
     * @param Momoger_Model_Abstract $model 系统状态信息模型
     * @param string|array $where 更新条件
     * @return Momoger_Model_Profile
     */
    public function update(Momoger_Model_Abstract $model, $where = null) {
    	if ($where) {
    		$this->getDao()->update($model->remove('pName')->getAll(), $where);
    	} else {
	    	$key = $model->get('pName');
	        if($key) {
	        	$row = $this->getDao()->fetchRow("pName = '$key'");
	        	if ($row) {
	        		$row->pValue = $model->get('pValue');
	        		$row->pDesc = $model->get('pDesc');
	        		$row->updated = time();
	        		$row->save();
	        		unset($row);
	        	}
	        }
    	}
		return $model;
    }

    /**
     * 删除模型数据
     *
     * @param Momoger_Model_Abstract $model 系统状态信息模型
     * @param string|array $where 删除条件
     * @return Momoger_Model_Profile
     */
    public function delete(Momoger_Model_Abstract $model, $where = null) {
    	if ($where) {
    		$this->getDao()->delete($where);
        	$model->reset();
    	} else {
	    	$key = $model->get('pName');
	        if($key) {
	        	$this->getDao()->delete("pName = '$key'");
	        	$model->reset();
	        }
    	}
		return $model;
    }

    /**
     * 更加模型身份ID获取模新保存在数据源中的数据, 成功找到更新模型本身数据.
     *
     * @param Momoger_Model_Abstract $model 模型对象
     * @param int|string $key 模型身份ID
     * @return Momoger_Model_Profile
     */
    public function find(Momoger_Model_Abstract $model, $key) {
    	return $this->findRow($model, "pName = '$key'");
    }

    /**
     * 获取数据表模型.
     *
     * @return Momoger_Dao_Profile
     */
    protected function getDao() {
        if (null === $this->_dao) {
            $this->setDao('Momoger_Dao_Profile');
        }
        return $this->_dao;
    }
}